package main

import (
	"fmt"
	"unsafe"
)

func main4() {
	s := "abcd"

	// 打印修改前的字符串信息
	fmt.Printf("修改前字符长度%d, 内容：%s\n", len(s), s)

	// 将字符串内存地址强制转换为unsafe.Pointer
	pointer := unsafe.Pointer(&s)

	// 将unsafe.Pointer强制转换为uintptr，从而向数字语义靠拢
	startAddress := (uintptr)(pointer)

	// 将unitptr类型的值加8，代表向高位地址偏移8
	lenAddress := startAddress + 8

	// 将uintptr转回到unsafe.Pointer，从而向对象语义靠拢
	targetPointer := unsafe.Pointer(lenAddress)

	// 将unsafe.Pointer强制转换为对象指针
	lenPointer := (*int)(targetPointer)

	// 修改对象指针的内容
	*lenPointer = 10

	//
	//dataPointer := unsafe.Pointer(dataAddress)
	//
	//u := (*uintptr)(dataPointer)
	//
	//targetAddress := dataAddress + 2
	//
	//targetPointer := unsafe.Pointer(targetAddress)
	//
	//bPointer := (*byte)(targetPointer)
	//
	//*bPointer = 'z'

	// 打印修改后的字符串信息
	fmt.Printf("修改后字符长度%d, 内容：%s\n", len(s), s)

}
